<?php
/**
 * @brief 响应类
 */
class Response {
    /**
     * 输出json
     *
     * @param array $array
     * @param boolean $isException
     * 
     * @return boolean
     */
    public static function output($array, $isException = false) {
        echo json_encode($array);
        if ($isException) {
            throw new Exception($array['message'], $array['code']);
        } else {
            return true;
        }
    }
    
    /**
     * 成功返回json
     *
     * @param array $data
     * @param string $message
     * @param int $code
     * 
     * @return boolean
     */
    public static function success($data = [], $message = 'Success', $code = 1) {
        $outArray = ['code' => $code, 'message' => $message, 'data' => json_encode($data)];
        return self::output($outArray);
    }

    /**
     * 错误返回json
     *
     * @param string $message
     * @param int $code
     * @param boolean $isException
     * 
     * @return boolean
     */
    public static function error($message, $code = 2, $isException = false) {
        $outArray = ['code' => $code, 'message' => $message];
        return self::output($outArray, $isException);
    }
}
